import VueRouter from 'vue-router' //引用路由器

//一级路由
import login from '../pages/login.vue'
import main from '../pages/main.vue'

import member from '../pages/member/member.vue'
import address from '../pages/member/address.vue'
import fadeback from '../pages/member/fadeback.vue'

import goods from "../pages/goods/goods.vue"
import goodstype from "../pages/goods/goodstype.vue"

import express from "../pages/order/express.vue"
import order from "../pages/order/order.vue"
import orderstate from "../pages/order/orderstate.vue"

import permission from "../pages/system/permission.vue"
import rolemanager from "../pages/system/rolemanager.vue"
import usermanager from "../pages/system/usermanager.vue"

import comment from "../pages/other/comment.vue"
import operatelog from "../pages/other/operatelog.vue"

//后台的欢迎页面
import welcome from "../pages/welcome.vue"


//前台路由页面
import MemberLogin from '../components/MemberLogin.vue'
import Index from '../components/Index.vue'
import ShopCart from '../components/ShopCart.vue'
import Favorites from '../components/Favorites.vue'
import PerCenter from '../components/PerCenter.vue'
import DailyRecom from '../components/DailyRecom.vue'
import Register from '../components/Register.vue'
import OrderCenter from '../components/OrderCenter.vue'

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
	return originalPush.call(this, location).catch(err => err)
};


let router = new VueRouter({
	routes: [
		// {
		//     path: '/',
		//     redirect: '/login'
		// },
		{
			path: "/login",
			component: login
		},
		{
			path: "/main",
			component: main,
			children: [{
					path: "/main/member",
					component: member,
				},
				{
					path: "/main/address",
					component: address,
				},
				{
					path: "/main/fadeback",
					component: fadeback,
				},
				{
					path: "/main/goods",
					component: goods,
				},
				{
					path: "/main/goodstype",
					component: goodstype,
				},
				{
					path: "/main/express",
					component: express,
				},
				{
					path: "/main/order",
					component: order,
				},
				{
					path: "/main/orderstate",
					component: orderstate,
				},
				{
					path: "/main/permission",
					component: permission,
				},
				{
					path: "/main/rolemanager",
					component: rolemanager,
				},
				{
					path: "/main/usermanager",
					component: usermanager,
				},
				{
					path: "/main/comment",
					component: comment,
				},
				{
					path: "/main/operatelog",
					component: operatelog,
				},
				// 进入后台的欢迎页面
				{
					path: '/main/welcome',
					component: welcome,
				},

			]
		},
		{
			path: "/Index",
			component: Index,
			// meta:{
			// 	 keepAlive: true
			// }
		},
		{
			path: '/ShopCart',
			name: 'ShopCart',
			component: ShopCart,
		},
		{
			path: '/Favorites',
			name: 'Favorites',
			component: Favorites,
		},
    {
    	path: '/OrderCenter',
    	name: 'OrderCenter',
    	component: OrderCenter,
    },
		//会员登录登录的路由组件
		{
			path: '/MemberLogin',
			name: 'MemberLogin',
			component: MemberLogin,
		},
		{
			path: '/PerCenter',
			name: 'PerCenter',
			component: PerCenter,
		},
		{
			path: '/Register',
			name: 'Register',
			component: Register,
		},
		

	]

});


export default router;



//路由跳转前执行beforeEach中代码
router.beforeEach(function(to, from, next) {
  console.log("目标路由地址为:", to);
  console.log("源路由地址为:", from);

  let regex = /^[/]main.*$/;
  if (regex.test(to.fullPath)) {
    //console.log("验证用户是否登录");

    let token = window.sessionStorage.getItem("token");
    //测试
    // let userInfo = 'admin';

    console.log("token的信息为:", token)
    if (token != null) {
      //console.log('token',token)
      //let userInfoObj = JSON.parse(userInfo);
      //解析成功后，应该再把用户名和密码传递给后端的Servlet，再次判断用户名和密码是否正确
      next(); //放行。
    } else {
      console.log("已经登录通行")
      router.replace({
        path: '/login'
      });
    }
  } else {
    next(); //放行。
    console.log("不验证用户是否登录")
  }

});